package com.blue.firecat.utils.excel;

import com.blue.firecat.utils.date.DateUtil;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.*;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;

/**
 * @Description: 导出excel
 * @Author: wanghuihui
 * @CreateDate: 2017-04-13
 */
public class ExportExcel {
    public static final Logger LOGGER = LoggerFactory.getLogger(ExportExcel.class);
    public static final String EXPORT_FILE = "导出文件";

    /**
     * 生成一Excel文件
     * @param fileName  文件名
     * @param sheetName  文件名
     * @param headers  第一列
     * @param response ??
     * @param dataSet
     * @return
     */
    public static String exportExcel(String fileName, String sheetName, String[] headers,
                                     final HttpServletResponse response, IDataWriteCallBack dataSet) {
        String temp = null;

        // 声明一个工作薄
        WritableWorkbook wwb;
        if (response == null) {
            return temp;
        }
        try {
            WorkbookSettings wbSetting = new WorkbookSettings();
            wbSetting.setUseTemporaryFileDuringWrite(true);
            //设置临时文件目录，防止发生内存泄露
            temp = FileUtil.getJxlTempDir() + File.separator + fileName + "_" +
                    DateUtil.convertDateToString(new Date(), "yyyyMMddHHmmss") + ".xls";
            wwb = Workbook.createWorkbook(new File(temp));
            if (wwb == null) {
                return temp;
            }
            // 生成一个表格
            WritableSheet sheet = wwb.createSheet(sheetName, 0);
            // 产生表格标题行
            for (int i = 0; i < headers.length; i++) {
                Label cell = new Label(i, 0, headers[i], setHeadStyle());
                sheet.setColumnView(i, 20);
                sheet.addCell(cell);
            }
            dataSet.writeData(sheet, wwb);
            wwb.write();
            wwb.close();

        } catch (Exception ioe) {
            LOGGER.error(ExceptionUtils.getStackTrace(ioe));
        }
        return temp;
    }

    /**
     * Jxl设置表头样式
     *
     * @return
     * @throws WriteException
     */
    public static WritableCellFormat setHeadStyle() throws WriteException {
        WritableFont wf = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false,
                UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        // 设置样式：center为居中
        WritableCellFormat cellFormat = new WritableCellFormat(wf);
        // 垂直居中
        cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
        // 水平居中
        cellFormat.setAlignment(Alignment.CENTRE);
        cellFormat.setBackground(Colour.YELLOW);
        //设置边框
        cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
        return cellFormat;
    }

    /**
     * Excel导出时，填写相关行的内容.
     *
     * @author zhaoxp
     */
    public interface IDataWriteCallBack {
        /**
         * @param sheet 回调函数,将需要的数据写入变量sheet中
         * @param wwb
         * @throws Exception
         */
        void writeData(WritableSheet sheet, WritableWorkbook wwb) throws Exception;
    }
}
